<?php
namespace app\hongyanche\controller;
class Api{
    //输出用户信息
	function user_info(){
	    $data = [
	        'code'=>input('code')
        ];
	    if(session('user_id')){
	        $user_id = session('user_id');
        }else{
	        if(!$data['code']){
	            return json(array('state'=>0,'errormsg'=>'please enter code'));
            }
            $demand = "user_info";
            $res = model('token')->check($demand,$data);
            if(isset($res['state']) && !$res['state']){
                return json(array('state'=>0,'errormsg'=>'Parameter error'));
            }
            $user_id = $res['data']['content'];
            session('user_id',$user_id);
        }
		if($user_id){
			return json(['state'=>1,'data'=>['userInfo'=>model('user')->getUserInfo($user_id)]]);
		}
		return json(['state'=>0,'errormsg'=>'code error']);
	}
    //跳转地址
    function redirect(){
        $urls = input('url');
        if(!$urls){
            return "Parameter error";
        }
        $app = "http://www.hongyanche.com/api/redirect?url=$urls";
        $demand ="user_info";
        $user = model('user');
        $user->wechatAutoLogin();
        $userId = $user->getUserId();
        if(!$userId){
            $url = "http://www.hongyanche.com/home?redirect=$app#login";
            header('Location:'.$url);die;
        }
        $data = [
            'content'=>$userId
        ];
        $where = "user_id = $userId and state =1";
        $tel = db('user_tel')
                ->field('tel')
                ->where($where)
                ->find();
        if(!$tel){
            $url = "http://www.hongyanche.com/home?redirect=$app#bind_tel";
            header('Location:'.$url);die;
        }
        $where = "user_id = $userId";
        $user_info = db('user')
                    ->field('district,city,province,real_name,identity_id')
                    ->where($where)
                    ->find();
        //判断车牌号和车辆品牌
        /*if(isset($user_info['vehicle_license_province']) && $user_info['vehicle_license_province'] == ""){
            $url = "http://www.hongyanche.com/home?redirect=$app#my";
            header('Location:'.$url);die;
        }
        if(isset($user_info['vehicle_license_number']) && $user_info['vehicle_license_number'] == ""){
            $url = "http://www.hongyanche.com/home?redirect=$app#my";
            header('Location:'.$url);die;
        }
        if(isset($user_info['vehicle_brand']) && $user_info['vehicle_brand'] == ""){
            $url = "http://www.hongyanche.com/home?redirect=$app#my";
            header('Location:'.$url);die;
        }*/
        if(isset($user_info['identity_id']) && $user_info['identity_id'] == ""){
            $url = "http://www.hongyanche.com/home?redirect=$app#my";
            header('Location:'.$url);die;
        }
        if(isset($user_info['real_name']) && $user_info['real_name'] == ""){
            $url = "http://www.hongyanche.com/home?redirect=$app#my";
            header('Location:'.$url);die;
        }
        if(isset($user_info['province']) && $user_info['province'] == ""){
            $url = "http://www.hongyanche.com/home?redirect=$app#my";
            header('Location:'.$url);die;
        }
        if(isset($user_info['district']) && $user_info['district'] == ""){
            $url = "http://www.hongyanche.com/home?redirect=$app#my";
            header('Location:'.$url);die;
        }
        if(isset($user_info['city']) && $user_info['city'] == ""){
            $url = "http://www.hongyanche.com/home?redirect=$app#my";
            header('Location:'.$url);die;
        }
        $res = model('token')->add($demand,$data);
        if(isset($res['state']) && !$res['state']){
            return json(array('state'=>0,'errormsg'=>'Parameter error'));
        }
        $code = $res['data']['code'];
        header('Location:'.$urls."?code=$code");die;
    }
}